<template>
	<view class="container">
		
		<view class="pageDr">
			<u-form :model="form" ref="uForm" label-width="180rpx" style="background-color: white;">
				<u-form-item label="收货人"   prop="name">
					<u-input v-model="form.name" placeholder="请输入收货人姓名" />
				</u-form-item>
				<u-form-item label="手机号码"  prop="tel">
					<u-input v-model="form.tel" type="number" placeholder="请输入手机号码" />
				</u-form-item>
				<u-form-item label="收货地址" prop="address"> 
					<u-input height="80rpx"  v-model="form.address" type="textarea" auto-height placeholder="请输入收货地址"> </u-input>
					<u-button @click="chooseMap" slot="right" type="success" size="mini">选择地址</u-button>
				</u-form-item>
				<u-form-item label="楼栋" prop="dong"><u-input type="number" v-model="form.dong" placeholder="请输入您所在的楼栋数" /></u-form-item>
				<u-form-item label="单元" prop="dy"><u-input type="number" v-model="form.dy" placeholder="请输入您所在的单元" /></u-form-item>
				<u-form-item label="楼层" prop="lc"><u-input type="number" v-model="form.lc" placeholder="请输入您所在的楼层" /></u-form-item>
				<u-form-item label="房号" prop="houseNumber"><u-input type="number" v-model="form.houseNumber" placeholder="请输入您所在的房号" /></u-form-item>
			</u-form>
			
			<view class="tips">请您如实填写以上信息</view>
			
		</view>	
		
		<view class="addBtn" @click="submit">
			<view class="bnts">立即绑定</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name: "",
					tel: "",
					address: "",
					weChatCode:"",
					dong:"",
					dy:"",
					lc:"",
					houseNumber: "",
				},
				
				rules: {
					name: [{required: true,message: '请输入收货人',trigger: ['change','blur'],}],
					tel : [
							{required: true,message: '请输入手机号',trigger: ['change','blur'],},
							{validator: (rule, value, callback) => {return this.$u.test.mobile(value);},message: '手机号码不正确',trigger: ['change','blur'],}
					],
					address: [{required: true,message: '请输入收货地址',trigger: ['change','blur'],}],
					dong: [{required: true,message: '请输入您所在的楼栋数',trigger: ['change','blur'],}],
					dy: [{required: true,message: '请输入您所在的单元',trigger: ['change','blur'],}],
					lc: [{required: true,message: '请输入您所在的楼层',trigger: ['change','blur'],}],
					houseNumber: [{required: true,message: '请输入门牌号',trigger: ['change','blur'],}],
				}
				
				
			};
		},
		
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		
		methods: {
			
			submit() {
				let  that = this;
				this.$refs.uForm.validate(valid => {
					if (valid) {
						that.postdata()
					} else {
						console.log('验证失败');
					}
				});
			},	
			
			postdata(){
				
				
				let   that = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event){
						const {code} = event
						//客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
						console.log(event)
						that.form.weChatCode = event.code;
						
						that.$u.api.addUser(that.form).then(res => {
							console.log(res);
							uni.showToast({
								title: res.msg,
								duration: 2000,
								icon:'none'
							});
							if(res.code == 0){ 
								setTimeout(()=>{
									that.onJump('/pages/home/home',4)
								},800)
							}
						})
						
					},
					fail: function (err) {
				        // 登录授权失败  
				        // err.code是错误码
				    }
				})
				
				
			},
			
			onJump(url,t){
				switch (t){
					case 1:
						// 保留当前页面，跳转到应用内的某个页面
						uni.navigateTo({url:url})
						break;
					case 2:
						// 关闭当前页面，跳转到应用内的某个页面。
						uni.redirectTo({url:url})
						break;	
					case 3:
						// 关闭所有页面，打开到应用内的某个页面。
						uni.reLaunch({url:url})
						break;
					case 4:
						// 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
						uni.switchTab({url:url})
						break;	
				}
			},  
			
			chooseMap() {
				uni.chooseLocation({
					success: (res) => {
						console.log(res);
						this.form.address = res.address;
					},
					complete(res) {
						console.log(res);
					},
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.container {padding: 32rpx;}
	.pageDr{background-color: white;}
	.addBtn{
	    position: fixed;width: 100%;bottom: 0;left: 0;text-align: center;background-color: #07c601;color: #fff;z-index: 999;
		.bnts{ height: 96rpx;line-height: 96rpx;font-size: 30rpx;}
	}
	
	@supports (padding-bottom: constant(safe-area-inset-bottom)) or (padding-bottom: env(safe-area-inset-bottom)){
		.addBtn{padding-bottom: calc(var(--window-bottom) + constant(safe-area-inset-bottom));padding-bottom: calc(var(--window-bottom) + env(safe-area-inset-bottom));}
	}
	
	.tips{margin-top: 25rpx;font-size: 24rpx; color: #F2994A;}
	
	
</style>